/* ********************************************************************************* */
/* 6 ***************** Inventor departure & Agency channel ************************* */
/* ********************************************************************************* */
/* Summary   : Code builds an unbalanced inventor firm year panel                    */
/* 			   and identify inventor movement                                        */
/* ********************************************************************************* */



	* Import STATA innovation data file innotest.dta after processing in Stata do file "3 innovation.do";
	PROC IMPORT out=innotest_processed  
	FILE= "C:\cow\innotest.dta" DBMS=STATA REPLACE;
	RUN;
	* Import STATA innovation data file inventor_stayer.dta after processing in Stata do file "4 inventor.do";
	PROC IMPORT out=move  
	FILE= "C:\cow\inventor_stayer.dta" DBMS=STATA REPLACE;
	RUN;
	data move; set move; 
	if move=1 and cites_raw>=90 then superstarmove=1; 
	if move=1; keep pyear permno move superstarmove ; run;

	* get median ownership and ind. dir;
	proc sort data=innotest_processed; by fyear; run;
	proc means noprint data=innotest_processed; by fyear; var ceoownership pct_inddir; 
	output out=innotest_avg (drop=_:) 
	median=ceoownership_p50 pct_inddir_p50 
	p25=ceoownership_p25 pct_inddir_p25
	p75=ceoownership_p75 pct_inddir_p75;
	run;
	* define hitech industries from FF5;
	data innotest_indavg; merge innotest_processed innotest_avg; by fyear;
	if ( sich ge 3570 and sich le 3579) or ( sich ge 3622 and sich le 3622) or ( sich ge 3660 and sich le 3692) or ( sich ge 3694 and sich le 3699) or ( sich ge 3810 and sich le 3839) or ( sich ge 7370 and sich le 7372) or ( sich ge 7373 and sich le 7373) or ( sich ge 7374 and sich le 7374) or ( sich ge 7375 and sich le 7375) or ( sich ge 7376 and sich le 7376) or ( sich ge 7377 and sich le 7377) or ( sich ge 7378 and sich le 7378) or ( sich ge 7379 and sich le 7379) or ( sich ge 7391 and sich le 7391) or ( sich ge 8730 and sich le 8734) or ( sich ge 4800 and sich le 4899) then FF3=1; else FF3=0;
	if ( sich ge 2830 and sich le 2839) or ( sich ge 3693 and sich le 3693) or ( sich ge 3840 and sich le 3859) or ( sich ge 8000 and sich le 8099) then FF4= 1; else FF4=0;
	if FF3=1 or FF4=1 then hitech=1; else hitech=0;
	run;

	%macro winsor(dsetin=, dsetout=, byvar=, vars=, type=, pctl=);
	%if &dsetout = %then %let dsetout = &dsetin; %let varL=; %let varH=; %let xn=1;
	%do %until ( %scan(&vars,&xn)= ); %let token = %scan(&vars,&xn); %let varL = &varL &token.L; %let varH = &varH &token.H; %let xn=%EVAL(&xn + 1); %end;%let xn=%eval(&xn-1); data xtemp; set &dsetin; run;
	%if &byvar = none %then %do;		data xtemp; set xtemp; xbyvar = 1; run;    %let byvar = xbyvar;	%end;
	proc sort data = xtemp; by &byvar; run;	proc univariate data = xtemp noprint; by &byvar; var &vars;	output out = xtemp_pctl PCTLPTS = &pctl PCTLPRE = &vars PCTLNAME = L H; run;
	data &dsetout; merge xtemp xtemp_pctl; by &byvar;    array trimvars{&xn} &vars; array trimvarl{&xn} &varL; array trimvarh{&xn} &varH;
	do xi = 1 to dim(trimvars); %if &type = winsor %then %do; if not missing(trimvars{xi}) then do; if (trimvars{xi} < trimvarl{xi}) then trimvars{xi} = trimvarl{xi}; if (trimvars{xi} > trimvarh{xi}) then trimvars{xi} = trimvarh{xi}; end; %end;
    %else %do; if not missing(trimvars{xi}) then do; if (trimvars{xi} < trimvarl{xi}) then delete; if (trimvars{xi} > trimvarh{xi}) then delete; end;%end; end; drop &varL &varH xbyvar xi; run;
	%mend winsor;

	%winsor(dsetin=innotest_indavg, dsetout=innotest_indavg_win, byvar=none, vars=q, type=winsor, pctl=1 99);

	proc univariate data=innotest_indavg_win; var q; run;

	data channel_1; set innotest_indavg_win;
	fnpatsassets=fnpats/assets; if fnpatsassets=. then fnpatsassets=0;
	patcountassets=patcount/assets; if patcountassets=. then patcountassets=0;
	ln_q=log(q);
	run;
	proc sort; by permno fyear; run;
	data channel_1; set channel_1;
	lag1_permno=lag(permno);
	lag2_permno=lag2(permno);
	lag3_permno=lag3(permno);
	lag4_permno=lag4(permno);
	lag5_permno=lag5(permno);
	lag1_ln_q=lag(ln_q);
	lag2_ln_q=lag2(ln_q);
	lag3_ln_q=lag3(ln_q);
	lag4_ln_q=lag4(ln_q);
	lag5_ln_q=lag5(ln_q);
	lag1_q=lag(q);
	lag2_q=lag2(q);
	lag3_q=lag3(q);
	lag4_q=lag4(q);
	lag5_q=lag5(q);
	run; 
	proc sort; by permno descending fyear; run;
	data channel_1; set channel_1;
	lead1_permno=lag(permno);
	lead2_permno=lag2(permno);
	lead3_permno=lag3(permno);
	lead4_permno=lag4(permno);
	lead5_permno=lag5(permno);
	lead1_ln_q=lag(ln_q);
	lead2_ln_q=lag2(ln_q);
	lead3_ln_q=lag3(ln_q);
	lead4_ln_q=lag4(ln_q);
	lead5_ln_q=lag5(ln_q);
	lead1_q=lag(q);
	lead2_q=lag2(q);
	lead3_q=lag3(q);
	lead4_q=lag4(q);
	lead5_q=lag5(q);
	run;


	data channel_1; set channel_1;
	chg5_ln_q=lead5_ln_q-lag5_ln_q;
	chg4_ln_q=lead4_ln_q-lag4_ln_q;
	chg3_ln_q=lead3_ln_q-lag3_ln_q;
	chg2_ln_q=lead2_ln_q-lag2_ln_q;
	chg1_ln_q=lead1_ln_q-lag1_ln_q;
	chg5_q=lead5_q-lag5_q;
	chg4_q=lead4_q-lag4_q;
	chg3_q=lead3_q-lag3_q;
	chg2_q=lead2_q-lag2_q;
	chg1_q=lead1_q-lag1_q;
	pchg5_q=lead5_q/lag5_q-1;
	pchg4_q=lead4_q/lag4_q-1;
	pchg3_q=lead3_q/lag3_q-1;
	pchg2_q=lead2_q/lag2_q-1;
	pchg1_q=lead1_q/lag1_q-1;
	drop lag: lead:;
	run; 
	data treat; set channel_1; 
	if ((incorp ="DE" and fyear=2000)
	 or (incorp ="OK" and fyear=2001)
	 or (incorp ="MO" and fyear=2003)
	 or (incorp ="KS" and fyear=2005)
	 or (incorp ="TX" and fyear=2006)
	 or (incorp ="NV" and fyear=2007)
	 or (incorp ="NJ" and fyear=2011)
	 or (incorp ="MD" and fyear=2014)
	 or (incorp ="WA" and fyear=2016)); 
	treat_id=_n_;
	run;

	* identify firms that lose inventors during the two years;
	proc sql; create table treat_move as select * from treat as a left join move as b 
	on a.permno= b.permno and a.fyear<=b.pyear<=a.fyear+1; quit;

	proc sort data=treat_move; by treat_id pyear; run;

	data treat_move_out; set treat_move; by treat_id; if first.treat_id; 
	if move=. then move=0; if superstarmove=. then superstarmove=0;
	run;

	PROC EXPORT DATA= treat_move_out OUTFILE= "C:\cow\incentive_move.dta" DBMS=STATA REPLACE;RUN;






